home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1997 May / PC Plus Super CD Issue 127 (May 1997).iso / handson / handson.exe / GLOBUNIT.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-02-01  |  1.4 KB  |  63 lines

  1. unit Globunit;
  2. { PC Plus sample Delphi program.  Illustrates the usage of global
  3.   variables.
  4.  
  5.   This uses a similar Proc1 procedure to the one in the FuncProc
  6.   project. However, in FuncProc the string variable, 's' is 'local'
  7.   to the Button1Click method and is not 'visible' to other procedures
  8.   unless it is specifically passed as a parameter.
  9.  
  10.   In the current project, 's' is declared 'globally' - at the top of the
  11.   unit and is, therefore, 'visible' to all procedures within the unit.
  12.   Now, when the Proc1 procedure makes s lower-case, s remains
  13.   lower-case when the Button1Click proc displays the string using the
  14.   line:
  15.     Edit2.Text := s;
  16.  
  17.   In general, avoid global variables. They can cause unexpected
  18.   side-effects. }
  19.  
  20.  
  21.  
  22. interface
  23.  
  24. uses
  25.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  26.   Forms, Dialogs, StdCtrls;
  27.  
  28. type
  29.   TForm1 = class(TForm)
  30.     Edit1: TEdit;
  31.     Edit2: TEdit;
  32.     Label1: TLabel;
  33.     Label2: TLabel;
  34.     Button1: TButton;
  35.     procedure Button1Click(Sender: TObject);
  36.   private
  37.     { Private declarations }
  38.   public
  39.     { Public declarations }
  40.   end;
  41.  
  42. var
  43.   Form1: TForm1;
  44.   s    : string; { s is now 'global' }
  45. implementation
  46.  
  47. {$R *.DFM}
  48.  
  49. procedure proc1;
  50. begin
  51.   s := LowerCase( s );
  52. end;
  53.  
  54. procedure TForm1.Button1Click(Sender: TObject);
  55. begin
  56.   s := Edit1.Text;
  57.   proc1;
  58.   Edit2.Text := s;
  59. end;
  60.  
  61.  
  62. end.
  63.